<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="queryEmp">
        <el-form-item>
          <el-input v-model="queryEmp.name" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="selectEmp">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addEmp">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="120" sortable>
      </el-table-column>
      <el-table-column prop="headImage" label="头像" width="180" sortable>
        <template scope="scope">
          <img :src="scope.row.headImage" width="80">
        </template>
      </el-table-column>
      <el-table-column prop="email" label="电子邮箱" width="180" sortable>
      </el-table-column>
      <el-table-column prop="age" label="年龄" sortable>
      </el-table-column>
      <el-table-column prop="department.name" label="所属部门" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="editEmp(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="delEmp(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        :total="total"
        style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑或修改界面-->
    <el-dialog :title="title" :visible.sync="empFormVisible" :close-on-click-modal="false">
      <el-form :model="empForm" label-width="80px" :rules="empFormRules" ref="empForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="empForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="empForm.age"></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱">
          <el-input v-model="empForm.email"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <el-input v-model="empForm.headImage"></el-input>
        </el-form-item>
        <el-form-item label="所属部门">
        <!--<el-input v-model="empForm.managerId"></el-input>-->
          <el-select v-model="empForm.department" value-key="id" clearable filterable placeholder="请选择">
            <el-option
                v-for="item in deps"
                :key="item.id"
                :label="item.name"
                :value="item">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="empFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="empFormSubmit" :loading="empFormLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import util from "../../common/js/util";
import {batchRemoveUser, editUser} from "../../api/api";

export default {
  name: "Employee",

  data(){
    return{
      queryEmp:{
        name: '',
      },
      employees: [], //员工
      listLoading: false, //加载
      sels: [], //选择多项
      total: 0, //Employee数据总数
      currentPage: 1, //当前默认页面
      pageSize: 10, //默认页面条数
      title: "新增", //表单名
      empFormVisible: false, //默认关闭弹出框
      //编辑或修改界面数据
      empForm: {
        id: null,
        username: null,
        password: null,
        email: null,
        headImage: null,
        age: null,
        departmentId: null,
        department: null
      },
      //表单验证
      empFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ]
      },
      empFormLoading: false, //不显示加载
      deps: [], //员工部门
      empTree: [], //级联选择
    }
  },

  methods: {
    //查询Employee
    selectEmp(){
      this.currentPage = 1
      this.listLoadingEmp()
    },
    //增加Employee
    addEmp(){
      //更改表单名
      this.title = "新增"
      //1.清空表单数据
      this.empForm = {
        id: null,
        username: null,
        password: null,
        email: null,
        headImage: null,
        age: null,
        departmentId: null,
        department: null
      }
      //2.打开弹出框
      this.empFormVisible = true
    },

    //选择项发生改变时触发
    selsChange(val){
      this.sels = val
    },

    //状态显示转换
    formatState: function (row, column) {
      return row.state == 1 ? '启用' : row.state == 0 ? '禁用' : '未知';
    },

    //编辑Employee数据
    editEmp(index,row){
      //更改表单名
      this.title = "编辑"
      //1.数据回显
      Object.assign(this.empForm,row)
      //2.打开弹出框
      this.empFormVisible = true
    },

    //删除Employee数据
    delEmp(index,row){
      //通过表单验证
      this.$confirm('确认删除数据吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.$http.delete("/emp/" + row.id).then((res) => {
          this.empFormLoading = false;
          if (res.data.success){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }
          else {
            this.$message.error(res.data.message)
          }
          this.listLoadingEmp()
        });
      });
    },

    //加载Employee列表数据
    listLoadingEmp(){
      //加载列表
      this.listLoading = true;
      //封装数据
      let query = {
        currentPage: this.currentPage, //当前页面
        pageSize: this.pageSize, //页面条数
        name: this.queryEmp.name, //关键字查询
      }
      //发送请求获取Employee列表数据
      this.$http.post('/emp',query).then(res=>{
        //数据总条数
        this.total = res.data.count
        //Employee列表数据
        this.employees = res.data.data
        //关闭加载列表
        this.listLoading = false;
      })
    },

    //批量删除
    batchRemove(){
      var ids = this.sels.map(item => item.id)
      this.$confirm('确认删除选中数据吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        // let para = { ids: ids };
        this.$http.patch("/emp",ids).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.listLoadingEmp();
        });
      }).catch(() => {

      });
    },

    //当前页条数
    handleSizeChange(val){
      this.pageSize = val
      this.listLoadingEmp()
    },

    //指向当前页
    handleCurrentChange(val){
      this.currentPage = val
      this.listLoadingEmp()
    },

    //提交弹出框
    empFormSubmit(){
      this.$refs.empForm.validate((valid) => {
        if (valid) {
          //通过表单验证
          this.$confirm('确认提交数据吗？', '提示', {}).then(() => {
            //用户点击了确认
            this.empFormLoading = true;

            this.$http.put("/emp",this.empForm).then((res) => {
              this.empFormLoading = false;
              if (res.data.success){
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
              }
              else {
                this.$message.error(res.data.message)
              }
              this.empFormVisible = false
              this.empFormLoading = false
              this.listLoadingEmp()
            });
          });
        }
      });
    },
    //获取员工所在部门
    loadDepartments(){
    //发送请求，获取员工所在部门
      this.$http.get("/dep").then(res => {
        this.deps = res.data
      })
    },

    //加载部门树，级联选择，父亲查儿子
    loadEmpTree(){
      this.$http.get("/emp/tree").then(res => {
        this.empTree = res.data
      })
    },
    handleChange(){

    }

  },

  mounted() {
    this.listLoadingEmp();
    this.loadDepartments();
  }
}
</script>

<style scoped></style>